
import bpy
import meshexport
import mypath
import myxml
import os

from myxml import *


def run() :
    run2(mypath.get_current_dir(bpy.data.filepath))

def run2(path) :

    basename = bpy.path.display_name(bpy.data.filepath)
    filename = path + os.sep + basename + ".scene.xml"
    file = open(filename, 'w')
    
    def w(text) :
        file.write(text)
    
    w(otag('scene'))
    
    for ob in bpy.data.objects:
    
        w(otag('object', [('name', ob.name)]))
        w(octag('coords', zip('xyz', ob.location)))
    
        if ob.get('terrain') is not None:
            # It is a terrain.
            meshname = basename + "_" + ob.name
            filename = path + os.sep + meshname + ".mesh.xml"
            meshexport.run(ob,filename)
            w(octag('terrain', [('mesh', meshname)]))
            
        if ob.get('entity') is not None:
            entity = ob.get('entity')
            w(octag('entity', [('name', entity)]))
        
        w(ctag('object'))
    
    w(ctag('scene'))